package com.jqb.webservice.ums.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.ws.WebServiceContext;

import com.jqb.webservice.ums.dao.UserDao;
import com.jqb.webservice.ums.service.IUserService;
import com.jqb.webservice.ums.vo.User;
import com.jqb.webservice.ums.vo.UserException;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.HeaderList;
import com.sun.xml.ws.developer.JAXWSProperties;
@WebService(endpointInterface="com.jqb.webservice.ums.service.IUserService",
			wsdlLocation="WEB-INF/wsdl/UserService.wsdl",
//			wsdlLocation="META-INF/wsdl/UserService.wsdl",
			targetNamespace="http://com.jqb.webservice.ums/UserService",
			serviceName="UserService",portName="UserServicePort")
public class UserService implements IUserService{
 
	private UserDao userDao = UserDao.newInstance();
	
	@Resource
	private WebServiceContext wsctx;
	
	public void checkLicence() throws UserException{
		HeaderList headers = (HeaderList) wsctx.getMessageContext().get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
		if(headers == null) throw new UserException("此项功能你无权操作...");
		QName qname = new QName("http://com.jqb.webservice.ums/UserService","licenceInfo");
		Header header = headers.get(qname,true);
		if(header == null) throw new UserException("此项功能你无权操作...");
		try {
			XMLStreamReader reader = header.readHeader();
			User user = new User();
			while(reader.hasNext()){
				int event = reader.next();
				if(event==XMLStreamReader.START_ELEMENT){
					String name = reader.getLocalName();
					if("id".equals(name)){
						user.setId(Integer.parseInt(reader.getElementText()));
					}
					if("username".equals(name)){
						user.setUsername(reader.getElementText());
					}
					if("password".equals(name)){
						user.setPassword(reader.getElementText());
					}
				}
			}
			User tuser = userDao.getUser(user.getId());
			if(tuser==null){
				throw new UserException("此授权用户不存在...");
			}if(!(user.getUsername().equals(tuser.getUsername()))||!(user.getPassword().equals(tuser.getPassword()))){
				throw new UserException("此授权用户名或密码错误...");
			}
		} catch (XMLStreamException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	@Override
	public void addUser(User user) throws UserException {
		checkLicence();
		System.out.println("addUser->sid:"+user.getId()+" sname:"+user.getUsername()+" spswd:"+user.getPassword());
		userDao.addUser(user);
	}

	@Override
	public void deleteUser(int id) {
		System.out.println("sid:"+id);
		userDao.deleteUser(id);
	}

	@Override
	public User getUser(int id) {
		System.out.println("sid:"+id);
		return userDao.getUser(id);
	}

	@Override
	public List<User> getUsers() {
		return userDao.getUsers();
	}

	@Override
	public void updateUser(User user) {
		System.out.println("updateUser->sid:"+user.getId()+" sname:"+user.getUsername()+" spswd:"+user.getPassword());
		userDao.updateUser(user);
	}

	@Override
	public User login(String username, String password) throws UserException  {
		System.out.println("login->sname:"+username+" spassword:"+password);
		return userDao.login(username, password);
	}

}
